/*
 * PanelCavas.java
 *
 * Created on 2008年3月29日, 下午8:19
 */

package GUI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

/**
 *
 * @author  littlechild
 */
public class PanelCavas extends javax.swing.JPanel {
    BufferedImage image;
    int width=0;
    int height=0;
    /** Creates new form PanelCavas */
    public PanelCavas(BufferedImage im) {
        super();
        initComponents();
        setImage(im);
        this.setBackground(Color.WHITE);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(image!=null) {
            if(width==0 || height ==0) {//not initiate the size 
                g.drawImage(image,0,0,this);
            }
            else {
                g.drawImage(image,0,0,width,height,this);
            }
        }
    }
    public BufferedImage getImage() {
        return image;
    }
    
    public void setImage(BufferedImage im) {
        image=im;
        if((width==0 || height==0) && im!=null) // not initiate the size
            this.setPreferredSize(new Dimension(image.getWidth(),image.getHeight()));
        repaint();
        java.awt.Container c=this;
        while (!(c instanceof java.awt.Window)) {
            if (c instanceof javax.swing.JComponent) {
                ((javax.swing.JComponent)c).updateUI();
            }
            c=c.getParent();
            if(c==null)
                break;
        }
    }
    public void setPictureSize(int w,int h) {
        this.width=w;
        this.height=h;
        //System.out.println("width:"+width+"; height"+height);
    }
}
